package com.simplecontact.adapters;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.simplecontact.R;
import com.simplecontact.classes.clsIconText;

public class adIconText extends BaseAdapter {

	private LayoutInflater mInflater;
	private int[] aId;
    private String[] aIcon;
    private String[] aData;
    private Context context;
    
    public adIconText(Context pcontext, String[] tIcon, String[] tData, int[] tId) {
        // Cache the LayoutInflate to avoid asking for a new one each time.
    	if(tIcon!=null){
    		aIcon = new String[tIcon.length];
    		for(int i = 0 ; i < tIcon.length ; i++)aIcon[i] = tIcon[i];
    	}
    	if(tData!=null){
    		aData = new String[tData.length];
        	for(int i = 0 ; i < tData.length ; i++)aData[i] = tData[i];
    	}    	
    	if(tId!=null){
    		aId = new int[tId.length];
    		for(int i = 0 ; i < tId.length ; i++)aId[i] = tId[i];
    	}
        mInflater = LayoutInflater.from(pcontext);                        
        context = pcontext;
    }
        
    public int getCount() {
        return aData.length;
    }
    
    public Object getItem(int position) {
        return position;
    }
    
    public long getItemId(int position) {
    	if(aId!=null)
    		return aId[position];
    	return position;
    }

    /**
     * Make a view to hold each row.
     *
     * @see android.widget.ListAdapter#getView(int, android.view.View,
     *      android.view.ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid unneccessary calls
        // to findViewById() on each row.
        clsIconText holder;

        // When convertView is not null, we can reuse it directly, there is no need
        // to reinflate it. We only inflate a new View when the convertView supplied
        // by ListView is null.
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_icon_text_grid, null);

            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new clsIconText();
            holder.setText((TextView) convertView.findViewById(R.id.icontext));
            holder.setIcon((ImageView) convertView.findViewById(R.id.gridicon));
            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (clsIconText) convertView.getTag();
        }
        // Bind the data efficiently with the holder.
        if(aData!=null)
        	holder.getText().setText(aData[position]);        
        if(aIcon==null){
        	holder.getIcon().setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.user));
        }
        else{
        	if(aIcon[position].length()!=0)
        		holder.getIcon().setImageBitmap(BitmapFactory.decodeFile(aIcon[position]));
        	else
        		holder.getIcon().setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.user));
        }
        return convertView;
    }
}
